package com.example.contactfragment;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.boyuan.huijiaoyu.R;
import com.example.newfragment.CTHAdapter;
import com.example.newfragment.RecentPerson;
import com.magicsoft.app.helper.StringUtils;
import com.magicsoft.constant.Constant;
import com.nostra13.universalimageloader.core.ImageLoader;

public class HomeContactOrgAdapter extends CTHAdapter<RecentPerson> {

	private ImageLoader imageLoader = ImageLoader.getInstance();

	public HomeContactOrgAdapter(Context context,
			List<RecentPerson> data,ImageLoader imageLoader) {
		super(context, data);
		// this.options = options;
		 this.imageLoader = imageLoader;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		RecentPerson contactEntity = data.get(position);

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.home_contact_org_item, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.imageview);
			holder.iv_separate = (ImageView) convertView
					.findViewById(R.id.iv_separate);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		String type = contactEntity.getType();

		if ("org".equals(type)) {
			holder.imageView.setVisibility(View.GONE);
			if (position != 0
					&& "user"
							.equals(((RecentPerson) getItem(position - 1))
									.getType())) {
				holder.iv_separate.setVisibility(View.VISIBLE);
				
			} else {
				holder.iv_separate.setVisibility(View.GONE);
			}
		} else if ("user".equals(type)) {
			holder.imageView.setVisibility(View.VISIBLE);
//			if (StringUtils.isImageLayout(contactEntity.getUid())) {
			if (contactEntity.getUid() != null) {
				String uri = Constant.photourl + contactEntity.getUid();
				imageLoader.displayImage(uri, holder.imageView);
			} else {
				holder.imageView.setImageResource(R.drawable.default_header);
			}
		}

		holder.tv_name.setText(contactEntity.getName());
		return convertView;
	}

	static class ViewHolder {
		ImageView iv_separate;
		ImageView imageView;
		TextView tv_name;
	}
}
